function [ ps ,ks,ts,rs ] = generatePs3D( fdiff, skew, ar,centerdeviation, numps,projective,silent,cdev_diff, WIDTH,HEIGHT,differentframes )
%if fdiff is 1 then all Fs will be different, if 0 otehrwise,
%skew is just the max skew allowed, if its zero them all skews is zero
%ar is the aspect ratio
% centerdeviation is how much the center will deviate from the image center
%numps is the number of the projection matrices
%cdev_diff indicates tht the camera centers are different
projective=1;
format long g;

rand('state', sum(100*clock));

if (nargin <11)
    differentframes=0;
end
curnumdifferent=0;

a = 100; b = 200;

ps = cell(1,numps) ;
ks = cell(1,numps) ;
rs = cell(1,numps) ;
ts = cell(1,numps) ;

minfw=0.2*WIDTH;
overallF=minfw+(rand()*WIDTH); % if fdiff is 0 then they are all set to this
curentF=0;

T=zeros(3,1);
R=eye(3,3);
prevF=overallF;
%cdev_diff=10;
deviatevar=centerdeviation/20;
xcenterdevgauss=(randn()*deviatevar);
ycenterdevgauss=(randn()*deviatevar);
convergent=(randn<0);
for i=1:numps
    
    if(convergent>0.5)
        g=-(randn(3,1)<0);
        g(g>-0.5)=1;
    else
        g=ones(3,1);
    end
    
    
    
    T= a + (b-a) * rand(3,1);
    T=T.*g;
    
    R= rotationmat3D((-pi + (pi*2) * rand()),[(rand()*2-1) (rand()*2-1) (rand()*2-1)]);
    
    %degeneracy maker
    %  R=eye(3);
    K=eye(3);
    K(1,2)=skew;
    
    if(differentframes==0)
        curentF=prevF;
    else
        curentF=abs(prevF);
        while(curentF<minfw || curentF>(1.5*WIDTH))
            curentF=abs(prevF+((rand()*fdiff)-(fdiff/2)));
        end
    end
    
    
    
    K(1,1)=curentF;
    K(2,2)=K(1,1)*ar;
    
    if(cdev_diff==0)
        K(1,3)=(WIDTH/2)+centerdeviation+xcenterdevgauss;
        K(2,3)=(HEIGHT/2)+centerdeviation+ycenterdevgauss;
    else
        K(1,3)=(WIDTH/2)+rand()*centerdeviation;
        K(2,3)=(HEIGHT/2)+rand()*centerdeviation;
    end
    
    if(curnumdifferent< differentframes)
        curnumdifferent=curnumdifferent+1;
        K(1,1)=K(1,1)+fdiff;
        K(2,2)=K(1,1)*ar;
        K(1,3)=K(1,3)+(rand()*(fdiff/10));
        K(2,3)=K(2,3)+(rand()*(fdiff/10));
    end
 %   R=eye(3,3);
 R= findlookrot(T);
    rs{1,i}=R;
    ts{1,i}=T;
    
    P=K*R*[eye(3,3) -T];
    
    ps{1,i}=P/norm(P);
    ks{1,i}=K;
    
    
    %now write all this to file
    
    if(silent~=1)
        disp(['forcal length ' num2str(i) ' is equal to: K[0][0] ' num2str(K(1,1)) ' or K[1][1] ' num2str(K(2,2))]);
        %         dirname=['projFolder' num2str(sum(round(100*clock)))];
        %         mkdir(dirname);
        %         save([dirname '/K' num2str(i) '.txt'], 'K','-ascii', '-double');
        %         save([dirname '/R' num2str(i) '.txt'], 'R','-ascii', '-double');
        %         save([dirname '/T' num2str(i) '.txt'], 'T','-ascii', '-double');
        %         save([dirname '/P' num2str(i) '.txt'], 'P','-ascii', '-double');
    end
    
    
end


end

function [Ri]= findlookrot(t)

% vlook=-t;
% vstick=[t(1) ; t(2) ; t(3)+100]-t;
% 
% vlook=norm(vlook);
% vstick=norm(vstick);
% 
% kb = cross(vlook,vstick);
% kb = norm(kb);


 u =-t;
 v = [0 ; 0 ;sqrt(t(1)^2 + t(2)^2 + t(3)^2 ) ] ;
 
 u1 = u/norm(u);
 v1 = v/norm(v);
 
 k = cross(u1,v1);
 % Rodrigues's formula:
 costheta = dot(u1,v1);
 R =[ 0 -k(3) k(2); 
 k(3) 0 -k(1);
 -k(2) k(1) 0];
 Ri = costheta*eye(3) + R + k*k'*(1-costheta)/sum(k.^2);


end